
/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：                                                    
*│　作    者：gaotang                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-01-04 10:01:02                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： BestTrader.Cms.Services                                  
*│　类    名： ManagerDataService                                    
*└──────────────────────────────────────────────────────────────┘
*/
using BestTrader.Cms.Core.Extensions;
using BestTrader.Cms.IRepository;
using BestTrader.Cms.IServices;
using BestTrader.Cms.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BestTrader.Cms.Services
{
    public class ManagerDataService: IManagerDataService
    {
        private readonly IManagerDataRepository _repository;
        private readonly IManagerRepository _managerRepository;
        private readonly IDESEncryptService _desEncryptService;

        public ManagerDataService(IManagerDataRepository repository, IManagerRepository managerRepository, IDESEncryptService desEncryptService)
        {
            _repository = repository;
            _managerRepository = managerRepository;
            _desEncryptService = desEncryptService;
        }

        /// <summary>
        /// 修改图片上传内容
        /// </summary>
        public BaseResult ModifyData(ManagerModifyDataModel item, String UserId)
        {
            var result = new BaseResult();
            var now = DateTime.Now;
            if (item.Id != 0)
            {
                var manager = _managerRepository.Get(item.Id);
                if (manager != null)
                {
                    //#region 1.验证用户名已存在

                    //var _user = _repository.Get($"select * from Manager where UserName='{ model.UserName }' ");
                    //if (_user != null)
                    //{
                    //    result.ResultCode = ResultCodeAddMsgKeys.RegisterExceptionCode;
                    //    result.ResultMsg = ResultCodeAddMsgKeys.RegisterExceptionMsg;
                    //    return result;
                    //}

                    //#endregion

                    //#region 2.验证身份证已存在

                    //var _card = _repository.Get($"select * from Manager where CardId='{ item.CardId }' ");
                    //if (_card != null)
                    //{
                    //    result.ResultCode = ResultCodeAddMsgKeys.RegisterCardIdExceptionCode;
                    //    result.ResultMsg = ResultCodeAddMsgKeys.RegisterCardIdExceptionMsg;
                    //    return result;
                    //}

                    //#endregion
                    //_mapper.Map(item, manager);
                    manager.Remark = item.Remark;
                    manager.Email = item.Email;
                    manager.Mobile = item.Mobile;
                    manager.NickName = item.NickName;
                    manager.BankType = item.BankType;
                    manager.BankCity = item.BankCity;
                    manager.BankProvince = item.BankProvince;
                    manager.BankCard = item.BankCard;
                    manager.CardId = item.CardId;
                    manager.QQ = _desEncryptService.DecryptQQ(manager.QQ);
                    manager.ModifyManagerId = UserId.ToInt32() ?? 0;
                    // 重置为未审核
                    manager.State = 0;
                    manager.ModifyTime = now;

                    manager = _desEncryptService.EncryptManagerInfo(manager);

                    if (_managerRepository.Update(manager) > 0)
                    {
                        if (item.UploadImages.Any())
                        {
                            _repository.DeleteList($"where ManagerId = { item.Id }");
                            foreach (var imageSrc in item.UploadImages)
                            {
                                _repository.Insert(new Models.ManagerData
                                {
                                    ImageUrl = imageSrc,
                                    ManagerId = item.Id,
                                    AddTime = now
                                });
                            }
                        }
                        result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                        result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
                    }
                    else
                    {
                        result.ResultCode = ResultCodeAddMsgKeys.CommonExceptionCode;
                        result.ResultMsg = ResultCodeAddMsgKeys.CommonExceptionMsg;
                    }
                }
                else
                {
                    result.ResultCode = ResultCodeAddMsgKeys.CommonFailNoDataCode;
                    result.ResultMsg = ResultCodeAddMsgKeys.CommonFailNoDataMsg;
                }
            }

            return result;
        }
    }
}